<?php
namespace app\admin\controller;
use think\Controller;
class ClassSingle extends Controller
{
    public function __construct()
    {
        parent::__construct();
        session_start();
        // $username = 
        // $username = $_SESSION['username'];
        if(empty($_SESSION['username'])) {
            // header('Location: http://127.0.0.1/bnstudy/public/index.php/admin/Admin/index');die;
            $this->wderror('http://127.0.0.1/bnstudy/public/index.php/admin/Admin/index', '请登录');die;
        }
        
         
    }
   public function index() {
       $csRes = model('ClassSingle')->field('cs.*,c.cor_name')->alias('cs')->join('course c', 'cs.course_id = c.id')->where('cs.status',1) ->paginate(1);
       $this->assign([
           'csRes'=>$csRes
       ]);
       return view('list');
   }
   //添加
   public function add() {
        if(request()->isPost()) {
            $data = input('post.');
            $csModel = model('ClassSingle');
            $add = $csModel->save($data);
            if($add) {
                $this->success('添加成功', 'index');
            }else {
                $this->error('添加失败');
            }
            return;
        }
        $id = input('id', 0);
        $corRes = model('course')->order('id desc')->select();
        $this->assign([
            'corRes'=>$corRes,
            'id'=>$id
        ]);
        return view('add');
   }
    //删除按钮
    public function del($id) {
        $del = model('ClassSingle')->save(['status'=>0],['id'=>$id]);
        if($del) {
         $this->success('删除成功', 'index');
        }else {
         $this->success('删除失败');
        }
    }
   //编辑
   public function edit() {
       $csModel = model('classSingle');
       $cs = $csModel->find(input('id'));
        if(request()->isPost()) {
            $data = input('post.');
            $add = $csModel->save($data,['id'=>$data['id']]);
            if($add) {
                $this->success('修改课时成功', 'index');
            }else {
                $this->error('修改课时失败');
            }
            return;
            }
            $corRes = model('course')->order('id desc')->select();
            $this->assign([
                'corRes'=>$corRes,
                'cs'=>$cs
            ]);
            return view();
        }
}
